﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.BL.BUS;
using QUANLYCHAMCONG.BL.Entities;
using Hopthoai;
using Microsoft.VisualBasic.CompilerServices;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace QUANLYCHAMCONG.PL.CMS.BaoCao
{
    public partial class Baocaogiochialamhai : UserControl
    {
        public delegate void _Close();
        public event _Close Close;
        public Baocaogiochialamhai()
        {
            InitializeComponent();
        }
        System.Data.DataTable dt = new System.Data.DataTable();
        public static Microsoft.Office.Interop.Excel.Application ExApp;
        public static Microsoft.Office.Interop.Excel.Workbook TasWBook;
        public static Microsoft.Office.Interop.Excel.Worksheet TasWSheet = new Microsoft.Office.Interop.Excel.Worksheet();
        public void loadPhongban()
        {
            System.Data.DataTable phongban = new System.Data.DataTable();
            phongban = PHONGBANBUS.SelectAll();
            if (phongban.Rows.Count != 0)
            {
                Common.AddDataForCombobox(ref this.cmbphongban, ref phongban, "TENPB", "MAPB");
            }
        }
        private void SetLink()
        {
            this.links1.SetValues("Báo cáo", "Giờ Ra vào");
        }

        private void Baocaogiochialamhai_Load(object sender, EventArgs e)
        {
            loadPhongban();
            SetLink();
        }
        public void TasStartExcel([Optional, DefaultParameterValue("Sheet1")] string SheetName)
        {
            try
            {
                ExApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                TasWBook = ExApp.Workbooks.Add(Missing.Value);
                TasWSheet = (Microsoft.Office.Interop.Excel.Worksheet)ExApp.Worksheets["Sheet1"];
                TasWSheet.Name = SheetName;
                ExApp.Visible = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        public System.Drawing.Point DoPhanGiaiManhinh()
        {
            System.Drawing.Point p = new System.Drawing.Point();
            Size s = SystemInformation.WorkingArea.Size;
            p.X = Size.Width / 2;
            p.Y = Size.Height / 2;
            return p;
        }
        private void btntimkiem_Click(object sender, EventArgs e)
        {
            string MAPB;
            if (cmbphongban.SelectedIndex != -1 && cmbphongban.SelectedValue.ToString() != "")
            {
                if (DateTime.Compare(dtpngaybd.Value, dtpkt.Value) < 0)
                {
                    MAPB = cmbphongban.SelectedValue.ToString();
                    if (!String.IsNullOrEmpty(MAPB))
                    {
                        BAOCAO bc = new BAOCAO();
                        bc.MAPB = MAPB;
                        bc.MANV = null;
                        bc.NGAYBD = dtpngaybd.Value.Date;
                        bc.NGAYKT = dtpkt.Value.Date;
                        frmwaiting f = new frmwaiting();
                        f.StartPosition = FormStartPosition.CenterScreen;
                        f.Show();
                        f.lbgoi.Text = "Đang duyệt....";
                        System.Windows.Forms.Application.DoEvents();
                        dt = BAOCAOBUS.CHECKINOUT(bc);
                        dscheckinout.DataSource = dt;
                        f.Close();
                    }
                }
                else
                {
                    frmHoidap hoidap = new frmHoidap("Không thể chọn ngày bắt đầu nhỏ hơn ngày kết thúc", Hopthoai.frmHoidap.CO.an, Hopthoai.frmHoidap.KHONG.an, Hopthoai.frmHoidap.THOAT.an, Hopthoai.frmHoidap.HINH.baoloi);
                    hoidap.ShowDialog();
                }

            }
        }
        void TaoHeaderForCheckINOUT()
        {
            Microsoft.Office.Interop.Excel.Worksheet tasWSheet = TasWSheet;
            //header 
            tasWSheet.get_Range("B" + Conversions.ToString((long)(2)) + ":H" + Conversions.ToString((long)(2)), Missing.Value).Merge(Missing.Value);
            tasWSheet.get_Range("B" + Conversions.ToString((long)(2)) + ":H" + Conversions.ToString((long)(2)), Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("B" + Conversions.ToString((long)(2)), Missing.Value).set_Value(Missing.Value, "BẢNG GIỜ CHECKIN - CHECKOUT");
            tasWSheet.get_Range("B" + Conversions.ToString((long)(2)), Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("B" + Conversions.ToString((long)(2)), Missing.Value).Font.Size = 16;
            tasWSheet.get_Range("B" + Conversions.ToString((long)(2)), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            //dong tiep theo
            //su ly data
            string tungay = dtpngaybd.Value.Day.ToString()+"-" + dtpngaybd.Value.Month.ToString() +"-"+ dtpngaybd.Value.Year.ToString();
            string dengnay = dtpkt.Value.Day.ToString() + "-"+dtpkt.Value.Month.ToString() +"-"+ dtpkt.Value.Year.ToString();
            tasWSheet.get_Range("B" + Conversions.ToString((long)(3)) + ":H" + Conversions.ToString((long)(3)), Missing.Value).Merge(Missing.Value);
            tasWSheet.get_Range("B" + Conversions.ToString((long)(3)) + ":H" + Conversions.ToString((long)(3)), Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("B" + Conversions.ToString((long)(3)), Missing.Value).set_Value(Missing.Value, "TỪ NGÀY : " + tungay + "      " + "ĐẾN NGÀY : " + dengnay);
            tasWSheet.get_Range("B" + Conversions.ToString((long)(3)), Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("B" + Conversions.ToString((long)(3)), Missing.Value).Font.Size = 11;
            tasWSheet.get_Range("B" + Conversions.ToString((long)(3)), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            tasWSheet.get_Range("B" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "STT");
            tasWSheet.get_Range("C" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Mã Nhân Viên");
            tasWSheet.get_Range("D" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Tên Nhân Viên");
            tasWSheet.get_Range("E" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Ngày");
            tasWSheet.get_Range("F" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Thứ");
            tasWSheet.get_Range("G" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Giờ vào");
            tasWSheet.get_Range("H" + Conversions.ToString(5), Missing.Value).set_Value(Missing.Value, "Giờ ra");
            tasWSheet.get_Range("B5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("B5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("B5", Missing.Value).Font.Size = 12;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["B", Missing.Value], null, "ColumnWidth", new object[] { 6 }, null, null, false, true);
            tasWSheet.get_Range("C5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            tasWSheet.get_Range("C5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("C5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("C5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("C5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["C", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
            tasWSheet.get_Range("D5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("D5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("D5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("D5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["D", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
            tasWSheet.get_Range("E5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("E5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("E5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("E5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["E", Missing.Value], null, "ColumnWidth", new object[] { 20 }, null, null, false, true);
            tasWSheet.get_Range("F5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("F5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("F5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("F5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["F", Missing.Value], null, "ColumnWidth", new object[] { 10 }, null, null, false, true);
            tasWSheet.get_Range("G5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("G5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("G5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("G5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["G", Missing.Value], null, "ColumnWidth", new object[] { 10 }, null, null, false, true);
            tasWSheet.get_Range("H5", Missing.Value).Borders.LineStyle = 1;
            tasWSheet.get_Range("H5", Missing.Value).Font.Bold = true;
            tasWSheet.get_Range("H5", Missing.Value).Font.Size = 12;
            tasWSheet.get_Range("H5", Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
            NewLateBinding.LateSetComplex(tasWSheet.Columns["G", Missing.Value], null, "ColumnWidth", new object[] { 10 }, null, null, false, true);
        }
        public void XuatGIONGUON(int vitri)
        {
            TasStartExcel("CHECKIN-CHECKOUT");
            int count = dt.Rows.Count;
            // doc do phan giai man hinh
            frmwaiting f = new frmwaiting();
            f.Show();
            f.lbgoi.Text = "Đang duyệt...";
            f.lbphantram.Visible = true;
            f.tocdo.Visible = true;
            f.tocdo.Maximum = count;
            f.Location = DoPhanGiaiManhinh();
            f.Activate();
            int i = 0;
            int j = 0;
            System.Windows.Forms.Application.DoEvents();
            if (dt.Rows.Count != 0)
            {
                try
                {
                    Microsoft.Office.Interop.Excel.Worksheet tasWSheet = TasWSheet;
                    TaoHeaderForCheckINOUT();
                    int final = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        f.tocdo.Value = i;
                        f.lbphantram.Text = Conversions.ToString((double)(Math.Round((double)(((double)i) / ((double)count)), 2) * 100.0)) + "%";
                        System.Windows.Forms.Application.DoEvents();
                        f.Activate();
                        string MANV = dr["MANV"].ToString();
                        string TENV = dr["TENNV"].ToString();
                        string dtngay = dr["NGAY"].ToString();
                        string thu = dr["THU"].ToString();
                        string Checkin = dr["CHECKIN"].ToString();
                        string checkout = dr["CHECKOUT"].ToString();
                        
                        if (String.IsNullOrEmpty(Checkin) && String.IsNullOrEmpty(checkout))
                        {
                            i++;
                            continue;

                        }
                        else
                        {
                            
                            tasWSheet.get_Range("B" + Conversions.ToString(j + 6) + ":H" + Conversions.ToString((int)(j + 6)), Missing.Value).Borders[XlBordersIndex.xlEdgeLeft].LineStyle = 1;
                            tasWSheet.get_Range("B" + Conversions.ToString(j + 6) + ":H" + Conversions.ToString((int)(j + 6)), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            tasWSheet.get_Range("B" + Conversions.ToString(j + 6) + ":H" + Conversions.ToString((int)(j + 6)), Missing.Value).Borders[XlBordersIndex.xlEdgeBottom].LineStyle = 3;
                            tasWSheet.get_Range("B" + Conversions.ToString(j + 6), Missing.Value).set_Value(Missing.Value, "'" + (j + 1).ToString());
                            tasWSheet.get_Range("B" + Conversions.ToString(j + 6), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                            tasWSheet.get_Range("B" + Conversions.ToString(j + 6),Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            tasWSheet.get_Range("C" + Conversions.ToString(j + 6), Missing.Value).set_Value(Missing.Value, "'" + MANV);
                            tasWSheet.get_Range("C" + Conversions.ToString(j + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            tasWSheet.get_Range("D" + Conversions.ToString(j + 6), Missing.Value).set_Value(Missing.Value, "'" + TENV);
                            tasWSheet.get_Range("D" + Conversions.ToString(j + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            tasWSheet.get_Range("E" + Conversions.ToString(j + 6), Missing.Value).set_Value(Missing.Value, "'" + dtngay);
                            tasWSheet.get_Range("E" + Conversions.ToString(j + 6), Missing.Value).HorizontalAlignment = XlHAlign.xlHAlignCenter;
                            tasWSheet.get_Range("E" + Conversions.ToString(j + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            tasWSheet.get_Range("F" + Conversions.ToString(j + 6), Missing.Value).set_Value(Missing.Value, "'" + thu);
                            tasWSheet.get_Range("F" + Conversions.ToString(j + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            tasWSheet.get_Range("G" + Conversions.ToString(j + 6), Missing.Value).set_Value(Missing.Value, "'" + Checkin);
                            tasWSheet.get_Range("G" + Conversions.ToString(j + 6), Missing.Value).Borders[XlBordersIndex.xlEdgeRight].LineStyle = 1;
                            tasWSheet.get_Range("H" + Conversions.ToString(j + 6), Missing.Value).set_Value(Missing.Value, "'" + checkout);
                            final = j;
                            j++;
                            i++;
                           
                        }
                    }

                  tasWSheet.get_Range("B" + Conversions.ToString(final + 7) + ":H" + Conversions.ToString((int)(final + 7)), Missing.Value).Borders[XlBordersIndex.xlEdgeTop].LineStyle = 1;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message.ToString());
                    f.Close();
                }
            }
            f.Close();
        }

        private void btnin_Click(object sender, EventArgs e)
        {
            XuatGIONGUON(1);
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            if (this.Close != null)
            {
                this.Close();
            }
        }
    }
}
